﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.DataModel
{
	public class HYPumpDataReader
	{
		public HYPumpDataReader()
		{
			this.sqlHelper = new SQLiteDataBaseHelper(Path.Combine(Product.DataLocation, "HYPumpData.db3"));
		}

		public DataTable GetFactoryTable()
		{
			DataTable tableSchema = this.sqlHelper.GetTableSchema(this.HYPUMP_FACTORY);
			if (tableSchema != null)
			{
				return tableSchema;
			}
			return null;
		}

		public DataTable GetPumpDataTable(string sTableName)
		{
			DataTable tableSchema = this.sqlHelper.GetTableSchema(sTableName);
			if (tableSchema != null)
			{
				return tableSchema;
			}
			return null;
		}

		public FamilySymbol GetFamilySymbolByFamilyNameInSub(string familyName, string tableName, Document doc, BuiltInCategory builtInCategory)
		{
			FamilySymbol result = null;
			string text = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(doc), tableName, familyName);
			Family family = null;
			SubTransaction subTransaction = null;
			try
			{
				foreach (Element element in YJKElementFilter.GetElementByCategory(doc, typeof(FamilySymbol), builtInCategory))
				{
					if ((element as FamilySymbol).Family.Name == familyName.Substring(0, familyName.IndexOf(".")))
					{
						FamilySymbol result2;
						result = (result2 = (element as FamilySymbol));
						return result2;
					}
				}
				subTransaction = new SubTransaction(doc);
				subTransaction.Start();
				doc.LoadFamily(text, out family);
				subTransaction.Commit();
			}
			catch (Exception)
			{
			}
			finally
			{
                if (subTransaction != null && subTransaction.GetStatus() != TransactionStatus.Committed)
				{
					subTransaction.RollBack();
				}
			}
			if (family != null)
			{
				using (List<FamilySymbol>.Enumerator enumerator2 = family.GetSymbols().GetEnumerator())
				{
					if (enumerator2.MoveNext())
					{
						result = enumerator2.Current;
					}
				}
			}
			return result;
		}

		public FamilySymbol GetFamilySymbolByFamilyNameInTran(string familyName, string tableName, Document doc, BuiltInCategory builtInCategory)
		{
			FamilySymbol result = null;
			string text = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(doc), tableName, familyName);
			Family family = null;
			Transaction transaction = null;
			try
			{
				foreach (Element element in YJKElementFilter.GetElementByCategory(doc, typeof(FamilySymbol), builtInCategory))
				{
					if ((element as FamilySymbol).Family.Name == familyName.Substring(0, familyName.IndexOf(".")))
					{
						FamilySymbol result2;
						result = (result2 = (element as FamilySymbol));
						return result2;
					}
				}
				transaction = new Transaction(doc, "加载族");
				transaction.Start();
				doc.LoadFamily(text, out family);
				transaction.Commit();
			}
			catch (Exception)
			{
			}
			finally
			{
                if (transaction != null && transaction.GetStatus() != TransactionStatus.Committed)
				{
					transaction.RollBack();
				}
			}
			if (family != null)
			{
				using (List<FamilySymbol>.Enumerator enumerator2 = family.GetSymbols().GetEnumerator())
				{
					if (enumerator2.MoveNext())
					{
						result = enumerator2.Current;
					}
				}
			}
			return result;
		}

		private readonly string HYPUMP_FACTORY = "PumpFactory";

		private SQLiteDataBaseHelper sqlHelper;
	}
}
